package mercede.allManagerBeans;

import mercede.allManagers.PersonpkManager;
import mercede.allTable.Personpk;
import mercede.allDTOs.personpkDTO;



import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;




/**
 * Session Bean implementation class PersonpkManagerBean
 */
@Stateless
public class PersonpkManagerBean implements PersonpkManager {
	

	@PersistenceContext
   private EntityManager em;
	
	@Resource
	private EJBContext context;
	

    @Override
    public void save(personpkDTO personpkdto) {
    	Personpk newpersonpk = new Personpk(personpkdto);
            em.persist(newpersonpk);
    }
    
    private Personpk find(int pid) {
    	   return em.find(Personpk.class, pid);
    	   }
    	    
    	   @Override
    	   public ArrayList<?> getPersonpk() {
    	   return (ArrayList<?>) em.createNamedQuery("Personpk.FIND_ALL", Personpk.class).getResultList();
    	   }
    	   
    	   
    	   @Override
    	   public personpkDTO getPersonpkDTO(int sid) {
    		   Personpk newPersonpk =find(sid);
    		   personpkDTO personpkdto = convertToDTO(newPersonpk);
    	       return personpkdto;
    	   }
    	   

    	   @Override
    	   public List<personpkDTO> getListPersonpkDTO() {
    	       List<Personpk> newPersonpk= em.createNamedQuery("Personpk.FIND_ALL", Personpk.class).getResultList();
    	       List<personpkDTO> personpkdto = new ArrayList<>();
    	       for(Personpk h: newPersonpk) {
    	    	   personpkdto.add(convertToDTO(h));
    	       }
    	       return personpkdto;
    	   }
    	   
    	   

    	   @Override
    	   public List<personpkDTO> getListPersonpkDTOByDest(String email) {
    		   
    	      Query query=em.createNamedQuery("Personpk.FIND_BY_MAIL", Personpk.class);
    	      query.setParameter("pusername",email);
    		   List<Personpk> newPersonpk= query.getResultList();
    	       List<personpkDTO> personpkdto = new ArrayList<>();
    	       for(Personpk h: newPersonpk) {
    	    	   personpkdto.add(convertToDTO(h));
    	       }
    	       return personpkdto;
    	   }
    	   
    	   public static List<personpkDTO> convertToDTOList(List<Personpk> PersonpkList) {
    	       List<personpkDTO> PersonpkdtoList = new ArrayList<personpkDTO>();
    	       for(Personpk h: PersonpkList){
    	    	   PersonpkdtoList.add(convertToDTO(h));
    	       }
    	       return  PersonpkdtoList;
    	}   
    	   
    	   
    	    public String getPrincipalName() {
    	        return context.getCallerPrincipal().getName();
    	    }
    	    
    	    public static  personpkDTO convertToDTO(Personpk Personpk)
    	    {
    	    	personpkDTO newPersonpkdto=new personpkDTO();
    	    	newPersonpkdto.setpID(Personpk.getPID());
    	    	newPersonpkdto.setpDeparture(Personpk.getPDeparture());
    	    	newPersonpkdto.setpDestination(Personpk.getPDestination());
    	    	newPersonpkdto.setpExcursion(Personpk.getPExcursion());
    	    	newPersonpkdto.setpFlight(Personpk.getPFlight());
    	    	newPersonpkdto.setpHotel(Personpk.getPHotel());
    	    	newPersonpkdto.setpNofdays(Personpk.getPNofdays());
    	    	newPersonpkdto.setpFName(Personpk.getPFName());
    	    	newPersonpkdto.setpLName(Personpk.getPLName());
    	    	newPersonpkdto.setpUsername(Personpk.getPUsername());
    	    	newPersonpkdto.setpStartdate(Personpk.getPStartdate());
    	    	
    	    	return newPersonpkdto;
    	    	
    	    }


    
    

}
